package com.xilihui.xlh.business.activitys;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.dialogs.MapDialog;
import com.xilihui.xlh.business.entities.ShopDetailEntity;
import com.xilihui.xlh.business.requests.HomeRequest;
import com.xilihui.xlh.business.util.AMapUtil;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.net.URISyntaxException;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/23.
 */

public class StoreDetailsActivity extends BaseCompatActivity implements XRecyclerView.LoadingListener, OnRefreshListener, OnLoadMoreListener {
   
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    Banner banner;
    TextView tv_name;
    TextView tv_appoint;
    RatingBar rb_pingjia;
    TextView tv_pengfei;
    TextView tv_intro;
    TextView tv_yuyue;
    TextView tv_time;
    TextView tv_address;
    ImageView iv_phone;
    
    String id = "";
    int page = 1;
    String sign_status = "";
    
    ShopDetailEntity.AgentBean agentBean;
    ArrayList<ShopDetailEntity.AgentBean.AgentImagesBean> images = new ArrayList<>();
    ArrayList<ShopDetailEntity.CommentBean> commentData = new ArrayList<>();
    BaseAdapter<ShopDetailEntity.CommentBean> beanBaseAdapter;
    
    @Override
    public int getLayout() {
        return R.layout.activity_store_details;
    }

    @Override
    public String initTitle() {
        return "门店详情";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        
        initHeadView();
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setLoadingListener(this);
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen.divider_line, LinearLayout.VERTICAL));
       
        beanBaseAdapter = new BaseAdapter<ShopDetailEntity.CommentBean>(this,commentData) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_shop_evaluate;
            }

            @Override
            protected void getView(BaseViewHolder holder, ShopDetailEntity.CommentBean entity, int position) {
                SimpleDraweeView imageView = (SimpleDraweeView)holder.getView(R.id.civ_userHead);
                imageView.setImageURI(UrlConst.baseUrl()+entity.getUser().getHead_pic());
                
                holder.setText(R.id.tv_nick,entity.getUser().getNickname());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                float zan = Float.valueOf(entity.getZan_num());
                holder.setRating(R.id.rb_pingfeng,zan);
                holder.setText(R.id.tv_user_pingfeng,zan+"分");
                holder.setText(R.id.tv_content,entity.getContent());
                
            }
            
        };
        beanBaseAdapter.setAnimationsLocked(true);
        recyclerView.setAdapter(beanBaseAdapter);
        
    }
    
    public void initHeadView(){
        View view = LayoutInflater.from(this).inflate(R.layout.recycler_shop_details_head,null);
        banner = (Banner)view.findViewById(R.id.banner_shop_img);
        tv_name = (TextView)view.findViewById(R.id.tv_name);
        tv_appoint = (TextView)view.findViewById(R.id.tv_woyaoyuyue); 
        rb_pingjia = (RatingBar)view.findViewById(R.id.rb_pingjia);
        tv_pengfei = (TextView)view.findViewById(R.id.tv_pengfei);
        tv_intro = (TextView)view.findViewById(R.id.tv_intro);
        tv_yuyue = (TextView)view.findViewById(R.id.tv_yuyue);
        tv_time = (TextView)view.findViewById(R.id.tv_time);
        tv_address = (TextView)view.findViewById(R.id.tv_address);
        iv_phone = (ImageView) view.findViewById(R.id.iv_phone);
        tv_appoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
                if(sign_status.equals("3")){
                    Intent intent = new Intent(StoreDetailsActivity.this,GoAppointmenActivity.class);
                    intent.putExtra("id",agentBean.getId());
                    intent.putExtra("shopName",agentBean.getName());
                    startActivity(intent);
                    finish();
                }else if(sign_status.equals("1")){
                    ToastUtil.toastShortNegative("审核中");
                }else {
                    startActivity(new Intent(StoreDetailsActivity.this,ApplyActivity.class));
                }
                
            }
        });
        
        tv_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
                MapDialog mapDialog = new MapDialog(StoreDetailsActivity.this,agentBean.getLat(),agentBean.getLng());
                mapDialog.show();
                
            }
        });
        
        iv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + agentBean.getPhone());
                intent.setData(data);
                startActivity(intent);
            }
        });
        recyclerView.addHeaderView(view);
    }

    @Override
    public void initData() {
        id = getIntent().getStringExtra("id");
        getData(true,page);
        
    }
    
    public void getData(boolean isLoading, final int page){

        HomeRequest.shopDetail(StoreDetailsActivity.this,Integer.parseInt(id),page)
                .compose(DoTransform.<ShopDetailEntity>applyScheduler(this,isLoading))
                .compose(StoreDetailsActivity.this.<ShopDetailEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ShopDetailEntity>(this) {
                    @Override
                    protected void onSuccess(ShopDetailEntity entity) {

                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page==1){
                            commentData.clear();
                            sign_status = entity.getSign_status();
                        }
                        
                        commentData.addAll(entity.getComment());
                        beanBaseAdapter.setList(commentData);
                        
                        agentBean = entity.getAgent();
                        if(agentBean!=null){
                            tv_name.setText(agentBean.getName());
                            float scroe = Float.valueOf(agentBean.getScore());
                            rb_pingjia.setRating(scroe);
                            tv_pengfei.setText(scroe+"分");
                            tv_intro.setText(agentBean.getRemark());
                            tv_yuyue.setText("预          约：选择门店一键预约到店不用等");
                            tv_time.setText("预约时间："+agentBean.getOpen_time()+"~"+agentBean.getClose_time());
                            tv_address.setText(agentBean.getAddress());
                            
                            images = (ArrayList<ShopDetailEntity.AgentBean.AgentImagesBean>) agentBean.getAgent_images();
                            
                            //设置banner样式
                            banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
                            //设置图片加载器
                            banner.setImageLoader(new ImageLoader() {
                                @Override
                                public void displayImage(Context context,Object path, ImageView imageView) {
                                    ShopDetailEntity.AgentBean.AgentImagesBean agentImagesBean = 
                                            (ShopDetailEntity.AgentBean.AgentImagesBean)path;
                                    ImageHelper.display(context,imageView,UrlConst.baseUrl()+agentImagesBean.getImage_url());     
                                }
                            });
                            
                            //设置图片集合
                            banner.setImages(images);
                            //设置自动轮播，默认为true
                            banner.isAutoPlay(true);
                            //设置轮播时间
                            banner.setDelayTime(1500);
                            //banner设置方法全部调用完毕时最后调用
                            banner.start();
                            
                            
                        }
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }


    @Override
    public void onRefresh() {
        
    }

    @Override
    public void onLoadMore() {
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false,page);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false,page);
    }
}
